﻿layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer;
    var count = $('#deliver_count').val();
    var surplus = 10 - parseInt(count);
    $('.deliver-left-count').text(surplus);

    $('#submit_bt').on('click', function () {
        $.ajax({
            url: '/ClientCenter/AddNewDiliverInfo',
            type: 'POST',
            async:false,
            data: {
                Name: $('#Name').val(),
                Address: $('#Deliver').val(),
                PhoneNumber:$('#PhoneNumber').val()
            },
            success: function (data) {
                if (data.success != "") {
                    layer.msg(data.success);
                    setTimeout(function () { window.location.reload(); }, 3000);
                }
                else if (data.error != "") {
                    layer.msg(data.error);
                }
            }
        })
    })

    $('.modify').click(function () {
        var id = $(this).parent().children('input[type="hidden"]').val();
        var index = layer.open({
            type: 2,
            title: '修改信息',
            area: ['400px', '350px'],
            shade: 0.2,
            scrollbar:false,
            maxmin: false,
            offset: [$(window).height() / 2 - 175, $(window).width / 2 - 200],
            content: "<div>"+ id +"</div>",
            btn: ['确认修改', '取消'],
            yes: function () {
                layer.msg('确认修改');
                layer.close(index);
            },
            btn2: function () {
                layer.close(index);
            }
        })
    })

    $('.delete').click(function () {
        var id = $(this).parent().children('input[type="hidden"]').val();
        var div = $(this).parent().parent();
        var index =  layer.confirm('是否删除此条地址', {
            btn:['删除','取消']
        }, function () {
            $.ajax({
                url: '/ClientCenter/DeleteDeliverInfo',
                type: 'POST',
                data: {
                    id:id,
                },
                success:function(data){
                    if (data.success != "") {
                        div.remove();
                        layer.msg(data.success, {
                            time: 3000
                        });
                    }
                    else if (data.error != "") {
                        layer.msg('删除失败,原因:' + data.error, {
                            time:3000,
                        })
                    }
                }
            })
            
        }
        , function () {
            layer.close(index);
        })
    })

    $('.default').click(function () {
        var id = $(this).prevAll("input[type='hidden']").val();
        var root = $(this).parent().parent();
        $.ajax({
            url: '/ClientCenter/SetDefaultDeliverInfo',
            method:'post',
            data:{deliverId :id},
            success: function (data) {
                if (data.success != "") {
                    layer.msg(data.success);
                    $('.blue').removeClass('blue');
                    root.addClass('blue');
                } else {
                    layer.msg(data.error);
                }
            }
        })
    })
})